import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

import 'package:qianyi/controller/homeTabController.dart';
import 'package:qianyi/entity/home/video_list_entity.dart';

import '../../view_model/home/recommend_video_list_view_model.dart';
import '../home/VideoListPage.dart';

class HomePage extends StatefulWidget {

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin<HomePage> ,SingleTickerProviderStateMixin {
  late PageController _pageController = PageController();
  HomeTabController _homeTabController = Get.find<HomeTabController>();
  @override
  void initState() {
    // TODO: implement initState
    _homeTabController.setTabController(this);
  }
  Widget _buildTabView() {
    List<Widget> tempList = [VideoListPage(pageController: _pageController,), const Text('7890')];
    // for (int i = 0; i < viewModel.tabs.length; i++) {
    //   tempList.add(_buildPageView(viewModel));
    // }

    return TabBarView(
      controller: _homeTabController.tabController,
      children: tempList,
    );
  }

  Widget _buildTabBar() {
    return SizedBox(
      width: double.infinity,
      height: 84,
      child: Stack(
        children: [
          Positioned(
            left: 0,
            bottom: 0,
            right: 0,
            height: 45,
            child: Center(
              child: TabBar(
                controller: _homeTabController.tabController,
                indicatorColor: Theme.of(context).brightness == Brightness.light
                    ? Colors.grey
                    : Colors.grey,
                indicatorSize: TabBarIndicatorSize.label,
                isScrollable: true,
                // labelColor: ColorsUtil.COLOR_DBDBDB,
                // unselectedLabelColor: ColorsUtil.COLOR_CDA4FF,
                indicatorWeight: 2.0,
                labelStyle: const TextStyle(
                    fontSize: 22, height: 2, fontWeight: FontWeight.w500),
                unselectedLabelStyle: const TextStyle(
                    fontSize: 22, height: 2, fontWeight: FontWeight.w500),
                tabs: _homeTabController.tabs.value.map((e) {
                  return Text(e);
                }).toList(),
              ),
            ),
          ),
        ],
      ),
    );
  }
  @override
  Widget build(BuildContext context) {
    super.build(context); // 重要：调用super.build(context)以保持状态
    return Container(
      child:  Stack(
        children: [
          _buildTabView(),

          _buildTabBar(),

        ],
      ),
    );


  }
  Widget _buildPageBottomContent(
      RecommendVideoListViewModel viewModel, int index, bool isLight) {
    VideoEntity entity = viewModel.datas[index];
    return  Container(
      width: 253, // 设置容器的宽度
      // color: Colors.red,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 确保文本与Expanded小部件的底部对齐
          Text(
              "@${entity.nickName}",
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                color: Colors.grey
                //
              )
          ),
          Container(height:2),
          Text(
            "${entity.title}" ?? "",
            style: TextStyle(fontSize: 16,
              fontWeight: FontWeight.bold,
                color: Colors.grey

            ),
            maxLines: 3, // 限制文本显示的行数
            overflow: TextOverflow.ellipsis, // 如果文本超出限制行数，则显示省略号
            softWrap: true, // 设置为true以启用自动换行
          ),

        ],
      ),
      // ],
      // ),
    );
  }

  @override
  bool get wantKeepAlive => true; // 重要：返回true以保持状态
}
